﻿namespace GuiaTest
{
    using System;
    using System.Threading;
    using Guia.Core;
    using Guia.Elements;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;

    /// <summary>
    /// Summary description for LabelTest
    /// </summary>
    [TestClass]
    public class MenuTest : AbstractWindowTest
    {
        protected override Type TestWindowType()
        {
            return typeof(MenuTestWindow);
        }

        protected override string TestWindowName()
        {
            return "MenuTestWindow";
        }

        [TestMethod]
        public void TestMenuNavigate()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem myMenuItem = menuMain["miExtras"]["miDetails"]["miDetail1"];
            Assert.AreEqual("Detail 1", myMenuItem.Name);
        }

        [TestMethod]
        [ExpectedException(typeof(ElementNotFoundException))]
        public void TestMenuNavigateWrongNoSubMenu()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem myMenuItem = menuMain["miExtras"]["miDetails"];
            MenuItem detailMenuItem = myMenuItem["miDetail7"];
        }

        [TestMethod]
        [ExpectedException(typeof(ElementNotFoundException))]
        public void TestMenuNavigateWrongSubMenuNotExpandeable()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem myMenuItem = menuMain["miExtras"]["miDetails"]["miDetail1"]["miSubDetail1"];
        }

        [TestMethod]
        public void TestShowMenu()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem[] extrasMenu = menuMain["miExtras"].GetAllMenuItems();
            Assert.AreEqual(3, extrasMenu.Length);
            Assert.AreEqual("Check 1", extrasMenu[0].Name);
            Assert.AreEqual("Check 2", extrasMenu[1].Name);
            Assert.AreEqual("Details", extrasMenu[2].Name);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestGetMenuItemWithNullValue()
        {
            Menu menuMain = window.GetMenu("menuMain");
            MenuItem myMenuItem = menuMain[null];
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestGetMenuItemWithEmptyValue()
        {
            Menu menuMain = window.GetMenu("menuMain");
            MenuItem myMenuItem = menuMain[""];
        }

        [TestMethod]
        public void TestInvokeMenu()
        {
            // TODO: Sleeps needed!!!
            Label validateLabel = window.Get<Label>("labelLastAction");
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            menuMain["miFile"]["miOpen"].Invoke();
            Delay.ForMilliseconds(100);
            Assert.AreEqual("Open", validateLabel.TextValue);
            menuMain["miFile"]["miSave"].Invoke();
            Delay.ForMilliseconds(100);
            Assert.AreEqual("Save", validateLabel.TextValue);
        }

        [TestMethod]
        public void TestCheckUncheckMenu()
        {
            Label validateLabel = window.Get<Label>("labelLastAction");
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            menuMain["miExtras"]["miCheck1"].Check();
            Assert.AreEqual("Checked", validateLabel.TextValue);
            Assert.AreEqual(true, menuMain["miExtras"]["miCheck1"].IsChecked);
            menuMain["miExtras"]["miCheck1"].UnCheck();
            Assert.AreEqual(false, menuMain["miExtras"]["miCheck1"].IsChecked);
        }

        [TestMethod]
        public void TestToggleMenu()
        {
            Label validateLabel = window.Get<Label>("labelLastAction");
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            menuMain["miExtras"]["miCheck1"].Toggle();
            Assert.AreEqual("Checked", validateLabel.TextValue);
            Assert.AreEqual(true, menuMain["miExtras"]["miCheck1"].IsChecked);
            menuMain["miExtras"]["miCheck1"].Toggle();
            Assert.AreEqual(false, menuMain["miExtras"]["miCheck1"].IsChecked);
        }

        [TestMethod]
        [ExpectedException(typeof(OperationNotSupportedException))]
        public void TestCheckNotSupportedMenu()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            Assert.AreEqual(false, menuMain["miFile"]["miOpen"].IsChecked);
            menuMain["miFile"]["miOpen"].Check();
        }

        [TestMethod]
        public void TestToggleWithInvoke()
        {
            Label validateLabel = window.Get<Label>("labelLastAction");
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            menuMain["miExtras"]["miCheck1"].Invoke();
            Assert.AreEqual("Checked", validateLabel.TextValue);
            Assert.AreEqual(true, menuMain["miExtras"]["miCheck1"].IsChecked);
            menuMain["miExtras"]["miCheck1"].Invoke();
            Assert.AreEqual(false, menuMain["miExtras"]["miCheck1"].IsChecked);
        }

        [TestMethod]
        [ExpectedException(typeof(OperationNotSupportedException))]
        public void TestExpandWithInvoke()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem mi = menuMain["miExtras"];
            mi.Invoke();
        }

        [TestMethod]
        public void TestExpandCollapseMenu()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem mi = menuMain["miExtras"];
            mi.Expand();
            Assert.AreEqual(true, mi.IsExpanded);
            mi.Collapse();
            Assert.AreEqual(false, mi.IsExpanded);
        }

        [TestMethod]
        public void TestExpandCollapseIsExpanded()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem mi = menuMain["miExtras"];
            mi.IsExpanded = true;
            Assert.AreEqual(true, mi.IsExpanded);
            mi.IsExpanded = false;
            Assert.AreEqual(false, mi.IsExpanded);
        }

        [TestMethod]
        [ExpectedException(typeof(OperationNotSupportedException))]
        public void TestExpandNotSupportedMenu()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem mi = menuMain["miFile"]["miSave"];
            mi.Expand();
        }

        [TestMethod]
        [ExpectedException(typeof(OperationNotSupportedException))]
        public void TestIsExpandedNotSupportedMenu()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem mi = menuMain["miFile"]["miSave"];
            Assert.AreEqual(false, mi.IsExpanded);
        }

        [TestMethod]
        [ExpectedException(typeof(OperationNotSupportedException))]
        public void TestIsCheckedNotSupportedMenu()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem mi = menuMain["miFile"]["miSave"];
            Assert.AreEqual(false, mi.IsChecked);
        }

        [TestMethod]
        public void TestNoSubmenu()
        {
            Menu menuMain = window.GetMenu("menuMain");
            Assert.IsNotNull(menuMain);
            MenuItem mi = menuMain["miFile"]["miSave"];
            MenuItem[] mis = mi.GetAllMenuItems();
            Assert.AreEqual(0, mis.Length);
        }
    }
}
